<?php
include_once(dirname(__FILE__)."/../inc/common.php"); 
$method = isset($_GET["method"])?$_GET["method"]:"";

if($method=="listQry"){
	$inOut = isset($_GET["subjectCate"])?$_GET["subjectCate"]:0;
	$showInvisible = isset($_GET["showInvisible"]) && ($_GET["showInvisible"]==1)?1:0;
	$tallyFlowTypeClass = new tally_flowType();
	$flowTypeList = $tallyFlowTypeClass->findFlowTypeByInout($userId, $inOut, $showInvisible);
	?>
	 <!--begin:数据列表-------------------------------------------------------------------------------------------------->
	    <form name="categroySchForm" id="categroySchForm" method="post">
        <input type="hidden" name="subjectCate" value="<?php echo $inOut; ?>">
	    <table id="listtb" class="listtable" cellpadding="0" cellspacing="1">
	      <thead>
	      <tr>
	        
	        <th width="5%">状态</th>	       
	        <th width="35%">名称</th>
            <th width="50%">说明</th>
	        <th width="10%">顺序</th>		        	                        
	        
	       
	    <!--     <th width="5%">操作</th>	-->                
	      </tr>
	      </thead>
	      <tbody>
		  <?php
			foreach($flowTypeList as $flowType){
			?>
	      <tr>
            <td> 
               <?php   if($flowType["isShow"]){ 
            				if($flowType["isDefault"]){
            	?>
            	<img style="padding-left: 1px;" src="../images/common/run.gif"/>
            	<?php 
            				}
            			}else{ ?>
            	<img style="padding-left: 1px;" src="../images/common/stop.gif"/>
            	<?php   } ?>
           </td>
	        <td style="text-align: left;padding-left: 36px;"><?php echo $flowType["name"]?></td>
			<td > </td>
	        <td>
		        
                   <label for="sort5772788" style="display:none">顺序</label>
		           <input type="text"  id="sort<?php echo $flowType["id"]?>" notnull="true" datatype="integer"  name="<?php echo $flowType["id"]?>" 
		           value="<?php echo $flowType["orderIndex"]?>" maxlength="5"   style="width:70px;text-align:center;border:0px;border-bottom:1px #000000 solid;"/>
		        
              
                <div class="oper_bar">
				    <span> <a  title="修改" href="javascript:fGoto();" onclick="Category.goEdit('<?php echo $flowType["id"]?>','<?php echo $inOut; ?>');">编辑  </a></span>
					<span> <a  title="删除" href="javascript:fGoto();" onclick="Category.onDelete('<?php echo $flowType["id"]?>','<?php echo $inOut; ?>');">删除  </a></span>
	                <?php if(!$flowType["isDefault"]){ ?>
	                	<span><a  title="<?php echo $flowType["isShow"]?"隐藏":"恢复"?>"  href="javascript:fGoto();" onclick="Category.alterHide('<?php echo $flowType["id"]?>','<?php echo $inOut; ?>','0','<?php echo $flowType["isShow"]?0:1?>');">
	                		<span><?php echo $flowType["isShow"]?"隐藏":"恢复"?></span></a></span>
	            		<span><a  title="设置为默认 "  href="javascript:fGoto();" onclick="Category.alterDefault('<?php echo $flowType["id"]?>','<?php echo $inOut; ?>','0','0');">设置为默认 </a></span>
	            	<?php } ?>
				</div>
	        </td> 
	       </tr>
	       <?php
			}
			?>
	       </tbody>	
	    </table>
             
           <a  id="Btn3"  title="显示隐藏的收支类别"  href="javascript:fGoto();" onclick="Category.showHidden('<?php echo $inOut; ?>','1');"><span>--显示隐藏的收支类别--</span></a>
           
           
	    </form>
	    <!--end:数据列表区------------------>	     
	<?php
}else if($method=="add"){
	?>
<div id="winwrapper" class="winwrapper" >
<form id="categoryAddForm" name="categoryAddForm"  method="post">
	<div class="content">
			<fieldset>
				<legend>基本信息</legend>
					<table cellpadding="0" cellspacing="1" class="form_table">
						<tr>
							<td class="caption"><label for="">&nbsp;</label></td>
							<td class="field">
								<input type="radio" name="subjectCate" <?php echo $_GET["subjectCate"]=="3"?"checked":""; ?> value="3" onclick="Category.changeCategory(this,'3');">收入
								<input type="radio" name="subjectCate" <?php echo $_GET["subjectCate"]=="4"?"checked":""; ?> value="4" onclick="Category.changeCategory(this,'4');">支出
							</td>
						</tr>
						<tr>
							<td class="caption"><label for="subjectName">名称:</label></td>
							<td class="field"><input id="subjectName" name="subjectName" type="text" class="text" value="" notnull="true" maxlength="30"><font color="red">*</font></td>
						</tr>
					</table>	
				   
			</fieldset>
	
	
	        <div class="buttons">
			        <input id="saveBtn" type="button" class="button"  value="确定" onclick="Category.save(this,'<?php echo $_GET["subjectCate"]?>');">
			        <input type="button" class="button"  value="取消" onclick="CM.close(this);return false;">	            
			</div>
			<div class="tip_wrapper" style="display:none">
				<a class="colse_btn" onclick="Help.goClose();" href="#" align="absmiddle"><img src="../images/icons/icon_close.gif"/></a>
				<span class="title">提示:</span>
				<span class="tip_content"></span>
			</div>
	</div>	    	
</form>
</div>
	<?php
}else if($method=="edit"){
	$oid = isset($_GET["oid"])?$_GET["oid"]:0;
	$tallyFlowTypeClass = new tally_flowType();
	$flowType = $tallyFlowTypeClass->findFlowTypeById($userId, $oid);
	?>
<div id="winwrapper" class="winwrapper">
	<form id="categoryEditForm" name="categoryEditForm"  method="post">
    <input type="hidden" name="categoryId" value="<?php echo $oid?>">
    <input type="hidden" name="subjectCate" value="<?php echo $flowType["flowType"]?>">
	<div class="content">
			<fieldset>
				<legend>基本信息</legend>
					<table cellpadding="0" cellspacing="1" class="form_table">
						<tr>
							<td class="caption"><label>&nbsp;&nbsp;</label></td>
							<td class="field" >
								<input type="radio" name="subjectCate" disabled  <?php echo $flowType["flowType"]=="3"?"checked":""; ?> value="3" >收入
								<input type="radio" name="subjectCate" disabled  <?php echo $flowType["flowType"]=="4"?"checked":""; ?> value="4" >支出
							</td>
						</tr>
						<tr>
							<td class="caption"><span class="em"></span><label for="subjectName">名称:</label></td>
							<td class="field"><input id="subjectName" name="subjectName" type="text" class="text" value="<?php echo $flowType["name"]?>" notnull="true" maxlength="30"></td>
						</tr>
					</table>	
				   
			</fieldset>
	        <div class="buttons">
			        <input id="saveBtn" type="button" class="button"  value="确定" onclick="Category.update(this,'<?php echo $flowType["flowType"]?>');">
			        <input type="button" class="button"  value="取消" onclick="CM.close(this);return false;">	            
			</div>	 
			<div class="tip_wrapper" style="display:none">
				<a class="colse_btn" onclick="Help.goClose();" href="#" align="absmiddle"><img src="../images/icons/icon_close.gif"/></a>
				<span class="title">提示:</span>
				<span class="tip_content"></span>
			</div> 
	</div>      
</form>
</div>
	<?php
}else if($method=="doAdd"){
	if(isset($_POST["subjectCate"]) && isset($_POST["subjectName"])){
		try{
			$inOut = isset($_POST["subjectCate"])?$_POST["subjectCate"]:0;
			$subjectName = isset($_POST["subjectName"])?$_POST["subjectName"]:"";
			$tallyFlowTypeClass = new tally_flowType();
			$tallyFlowTypeClass->createFlowType(
				$userId, $subjectName, $inOut
			);
			$result = array(
				"returnCode" => 0,
				"returnMsg" => "新增成功",
			);
		}catch(Exception $e){
			$result = array(
				"returnCode" => 2,
				"returnMsg" => $e->getMessage()?$e->getMessage():"新增失败",
			);
		}
	}else{
		$result = array(
			"returnCode" => 1,
			"returnMsg" => "参数错误",
		);
	}
	$jsonHandle = new ext_jsonHandle();
	$jsonHandle->jsonOut($result);
	exit();
}else if($method=="doEdit"){
	if(isset($_POST["categoryId"]) && $_POST["categoryId"] 
			&& isset($_POST["subjectCate"]) && $_POST["subjectCate"] 
			&& isset($_POST["subjectName"]) && $_POST["subjectName"]){
		try{
			$tallyFlowTypeClass = new tally_flowType();
			$flowType = $tallyFlowTypeClass->findFlowTypeById($userId, $_POST["categoryId"]);
			if($flowType){
				$flowType["name"] = $_POST["subjectName"];
				$tallyFlowTypeClass->updateFlowTypeInfo($userId, $_POST["categoryId"], $flowType);
				$result = array(
					"returnCode" => 0,
					"returnMsg" => "修改成功",
				);
			}else{
				throw new accountException("此分类不存在");
			}
		}catch(Exception $e){
			$result = array(
				"returnCode" => 2,
				"returnMsg" => $e->getMessage()?$e->getMessage():"修改失败",
			);
		}
	}else{
		$result = array(
			"returnCode" => 1,
			"returnMsg" => "参数错误",
		);
	}
	$jsonHandle = new ext_jsonHandle();
	$jsonHandle->jsonOut($result);
	exit();
}else if($method=="alterHide"){
	if(isset($_GET["oid"]) && $_GET["oid"]){
		try{
			$tallyFlowTypeClass = new tally_flowType();
			$flowType = $tallyFlowTypeClass->findFlowTypeById($userId, $_GET["oid"]);
			if($flowType){
				$flowType["isShow"] = $flowType["isShow"]?0:1;
				$tallyFlowTypeClass->updateFlowTypeInfo($userId, $_GET["oid"], $flowType);
				$result = array(
					"returnCode" => 0,
					"returnMsg" => "修改成功",
				);
			}else{
				throw new accountException("此分类不存在");
			}
		}catch(Exception $e){
			$result = array(
				"returnCode" => 2,
				"returnMsg" => $e->getMessage()?$e->getMessage():"修改失败",
			);
		}
	}else{
		$result = array(
			"returnCode" => 1,
			"returnMsg" => "参数错误",
		);
	}
	$jsonHandle = new ext_jsonHandle();
	$jsonHandle->jsonOut($result);
	exit();
}else if($method=="alterDefault"){
	if(isset($_GET["oid"]) && $_GET["oid"]){
		try{
			$tallyFlowTypeClass = new tally_flowType();
			$flowType = $tallyFlowTypeClass->findFlowTypeById($userId, $_GET["oid"]);
			if($flowType){
				$tallyFlowTypeClass->setDefaultFlowType($userId, $flowType["flowType"], $_GET["oid"]);
				$result = array(
					"returnCode" => 0,
					"returnMsg" => "修改成功",
				);
			}else{
				throw new accountException("此分类不存在");
			}
		}catch(Exception $e){
			$result = array(
				"returnCode" => 2,
				"returnMsg" => $e->getMessage()?$e->getMessage():"修改失败",
			);
		}
	}else{
		$result = array(
			"returnCode" => 1,
			"returnMsg" => "参数错误",
		);
	}
	$jsonHandle = new ext_jsonHandle();
	$jsonHandle->jsonOut($result);
	exit();
}else if($method=="delete"){
	if(isset($_GET["oid"]) && $_GET["oid"]){
		try{
			$tallyFlowTypeClass = new tally_flowType();
			$flowType = $tallyFlowTypeClass->findFlowTypeById($userId, $_GET["oid"]);
			if($flowType){
				$tallyFlowTypeClass->removeFlowType($userId, $_GET["oid"]);
				$result = array(
					"returnCode" => 0,
					"returnMsg" => "删除成功",
				);
			}else{
				throw new accountException("此分类不存在");
			}
		}catch(Exception $e){
			$result = array(
				"returnCode" => 2,
				"returnMsg" => $e->getMessage()?$e->getMessage():"删除失败",
			);
		}
	}else{
		$result = array(
			"returnCode" => 1,
			"returnMsg" => "参数错误",
		);
	}
	$jsonHandle = new ext_jsonHandle();
	$jsonHandle->jsonOut($result);
	exit();
}else if($method=="saveSort"){
	try{
		$tallyFlowTypeClass = new tally_flowType();
		foreach($_POST as $flowTypeId=>$orderIndex){
			$flowTypeId+=0;
			$orderIndex+=0;
			if($flowTypeId>0){
				$flowType = $tallyFlowTypeClass->findFlowTypeById($userId, $flowTypeId);
				if($flowType){
					$flowType["orderIndex"] = $orderIndex;
					$tallyFlowTypeClass->updateFlowTypeInfo($userId, $flowTypeId, $flowType);
				}
			}
		}
		$result = array(
			"returnCode" => 0,
			"returnMsg" => "保存顺序成功",
		);
	}catch(Exception $e){
		$result = array(
			"returnCode" => 2,
			"returnMsg" => $e->getMessage()?$e->getMessage():"保存顺序失败",
		);
	}
	$jsonHandle = new ext_jsonHandle();
	$jsonHandle->jsonOut($result);
	exit();
}else{
?>
		<!-- begin:导航条 -->
		<div class="titlebar">
		   <h3>收支类别</h3>
		</div>
		<!-- end:导航条 -->
		
	<div class="tab_wrapper">

    <form id="schForm"  method="post">
    <input type="hidden" id="subjectCatehidden" name="subjectCatehidden" value=""/>
    <div class="head">
			<ul class="tabs">
				<li class="active">
					<a href="javascript:fGoto();" id="subjectCateInCome" name="subjectCate" title="收入" onclick="Tab.activeLi(this);Category.search('3');Help.show(4026,0);"><span>收入</span></a>
				</li>
				<li>
					<a href="javascript:fGoto();" id="subjectCateExpense" name="subjectCate" title="支出" onclick="Tab.activeLi(this);Category.search('4');Help.show(4027,0);"><span>支出</span></a>
				</li>
			</ul>
	 </div> 
     </form> 
     <div class="body">	
		 <!-- begin:搜索区 -->
                <div class="toolbar">
				    <p>
					  <a  class="button" title="新增分类"  href="javascript:fGoto();" onclick="Category.goAdd();"><span>新增分类</span></a>
				      <a  id="sortBtn" class="button" title="保存排序"  href="javascript:fGoto();" onclick="Category.saveSort('s');"><span>保存顺序</span></a>		  	  
                     </p>
				</div>
	      <!-- end:搜索区 -->
		<!-- begin:-------数据区 -------------------->
		<div id="dataView" class="data list_area">
		     <div  align="center"><img align="absmiddle" src='../images/common/loading1.gif'></div>
		</div>
		<!--end: ---------数据区------------------->
	 
	</div>
	</div>

<?php
}
?>
